import data from "./data.js"
import { addScript, sleep, getCodeData, calculateMACD, calculateKDJ } from "./common.js"

const getSHjysCode = () => {
    return new Promise((resolve,reject) => {
        window[`jsonpCallback84379775`] = (res) => {
            console.log(JSON.stringify(res.list))
            resolve(res.list)
        }

        addScript("https://yunhq.sse.com.cn:32042/v1/sh1/list/exchange/equity?callback=jsonpCallback84379775")
    })
}

const judgment = (list) => {
    let result = false
    let last_index = list.length - 1

    let sign = 7;

    if(sign == 1){
        if(
            list[last_index] > list[last_index - 1] &&
            list[last_index - 1] < list[last_index -2] &&
            list[last_index -2] < list[last_index - 3] &&
            list[last_index -3] < list[last_index - 4] &&
            list[last_index -4] < list[last_index - 5]
        ){
            result = true;
        }
    } else if(sign == 2){
        if(
            list[last_index] > list[last_index - 1] &&
            list[last_index - 1] < list[last_index -2] &&
            list[last_index -2] < list[last_index - 3] &&
            list[last_index -3] < list[last_index - 4] &&
            list[last_index -4] < list[last_index - 5] &&
            list[last_index -5] < list[last_index - 6]
        ){
            result = true;
        }
    } else if(sign == 3){
        if(
            list[last_index] > list[last_index - 1] &&
            list[last_index - 1] < list[last_index -2] &&
            list[last_index -2] < list[last_index - 3] &&
            list[last_index -3] < list[last_index - 4] &&
            list[last_index -4] < list[last_index - 5] &&
            list[last_index -5] < list[last_index - 6] &&
            list[last_index -6] < list[last_index - 7]
        ){
            result = true;
        }
    } else if(sign == 4){
        if(
            list[last_index] > list[last_index - 1] &&
            list[last_index - 1] < list[last_index -2] &&
            list[last_index -2] < list[last_index - 3] &&
            list[last_index -3] < list[last_index - 4] &&
            list[last_index -4] < list[last_index - 5] &&
            list[last_index -5] < list[last_index - 6] &&
            list[last_index -6] < list[last_index - 7] &&
            list[last_index -7] < list[last_index - 8]
        ){
            result = true;
        }
    } else if(sign == 5){
        if(
            list[last_index] > list[last_index - 1] &&
            list[last_index - 1] < list[last_index -2] &&
            list[last_index -2] < list[last_index - 3] &&
            list[last_index -3] < list[last_index - 4] &&
            list[last_index -4] < list[last_index - 5] &&
            list[last_index -5] < list[last_index - 6] &&
            list[last_index -6] < list[last_index - 7] &&
            list[last_index -7] < list[last_index - 8] &&
            list[last_index -8] < list[last_index - 9]
        ){
            result = true;
        }
    } else if(sign == 6){
        if(
            list[last_index] > list[last_index - 1] &&
            list[last_index - 1] < list[last_index -2] &&
            list[last_index -2] < list[last_index - 3] &&
            list[last_index -3] < list[last_index - 4] &&
            list[last_index -4] < list[last_index - 5] &&
            list[last_index -5] < list[last_index - 6] &&
            list[last_index -6] < list[last_index - 7] &&
            list[last_index -7] < list[last_index - 8] &&
            list[last_index -8] < list[last_index - 9] &&
            list[last_index -9] < list[last_index - 10]
        ){
            result = true;
        }
    } else if(sign == 7){
        if(
            list[last_index] < list[last_index - 1] &&
            list[last_index - 1] < list[last_index -2] &&
            list[last_index -2] < list[last_index - 3] &&
            list[last_index -3] < list[last_index - 4] &&
            list[last_index -4] < list[last_index - 5] &&
            list[last_index -5] < list[last_index - 6] &&
            list[last_index -6] < list[last_index - 7] &&
            list[last_index -7] < list[last_index - 8] &&
            list[last_index -8] < list[last_index - 9] &&
            list[last_index -9] < list[last_index - 10]
        ){
            result = true;
        }
    }
    return result
}

const getDataByJsonp = async (url, callback) => {
    return new Promise(async (resolve, reject) => {
        window[`${callback}`] = async (res) => {
            resolve(res)
        }

        try {
            var urlWithRandom = `${url}?random=${Math.random()}`
            await addScript(urlWithRandom)
        } catch (error) {
            try {
                await sleep(5000)
                var urlWithRandom = `${url}?random=${Math.random()}`
                await addScript(url)
            } catch (error) {
                await sleep(5000)
                var urlWithRandom = `${url}?random=${Math.random()}`
                await addScript(urlWithRandom)
            }
        }
    })
}

const getMacdDataByParams = async (code_list, sign) => {
    return new Promise(async (resolve, reject) => {
        let macd_arr = []
        let kdj_arr = []
        let macd_kdj_arr = []
        let macd_kdj_0_arr = []
        for (let i = 0; i < code_list.length; i++) {
            const code = code_list[i][0];
            const name = code_list[i][1];

            if(String(code).startsWith("300") || String(code).startsWith("301")) continue

            try {
                let code_sign = `${sign}_${code}`
                // let urlToday = `https://d.10jqka.com.cn/v6/line/${code_sign}/11/defer/today.js`;
                // let callbackToday = `quotebridge_v6_line_${code_sign}_11_defer_today`;
                // let resToday = await getDataByJsonp(urlToday, callbackToday);

                // let todayData = {
                //     date: resToday[code_sign][1],
                //     start_price: resToday[code_sign][7],
                //     end_price: resToday[code_sign][11],
                //     high_price: resToday[code_sign][8],
                //     low_price: resToday[code_sign][9],
                //     volumn: resToday[code_sign][13]
                // }

                let url = `https://d.10jqka.com.cn/v6/line/${code_sign}/01/all.js`;
                let callback = `quotebridge_v6_line_${code_sign}_01_all`;
                let res = await getDataByJsonp(url, callback);


                // 股票50日的数据
                let code_data = await getCodeData(res.dates, res.price, res.volumn, 50)

                if(code_data.length > 0){
                    // code_data.push(todayData)
                    // 获取连续50日的尾盘价格
                    // 计算MACD
                    let last_price_50 = code_data.map(c => c.end_price)
                    let macd = calculateMACD(last_price_50)
                    if(judgment(macd.macd)){
                        console.log("macd", name)
                        macd_arr.push(name)

                        // 计算KDJ
                        let kdj = calculateKDJ(code_data)
                        let j_arr = kdj.map(k => k.j)
                        if(judgment(j_arr)){
                            
                            macd_kdj_arr.push(name)
                            console.log("macd and kdj", name)

                            // if(j_arr[j_arr.length - 2] < 0){
                            //     console.log("macd and kdj and (kdj - 1) < 0", name)
                            //     macd_kdj_0_arr.push(name)
                            // }
                            if(j_arr[j_arr.length - 1] < 0){
                                console.log("macd and kdj and (kdj - 1) < 0", name)
                                macd_kdj_0_arr.push(name)
                            }
                        }
                    }


                    // 计算KDJ
                    let kdj = calculateKDJ(code_data)
                    let j_arr = kdj.map(k => k.j)

                    if(judgment(j_arr)){
                        console.log("kdj", name)
                        kdj_arr.push(name)
                    }
                }
            } catch (error) {
                
            }

        }
        resolve({
            macd_arr,
            kdj_arr,
            macd_kdj_arr,
            macd_kdj_0_arr
        })
    })
}

const main = async () => {
    // let code_list = await getCode()
    // console.log(data.shjys_data, data.szjys_data)
    let sz_code_list = data.szjys_data;
    let sh_code_list = data.shjys_data;
    
    let sign = "hs";

    let sz_name_arr = await getMacdDataByParams(sz_code_list, sign)
    window.sz_name_arr = sz_name_arr
    console.log(sz_name_arr)

    // let sh_name_arr = await getMacdDataByParams(sh_code_list, sign)
    // window.sh_name_arr = sh_name_arr
    // console.log(sh_name_arr)
}

export {
    getSHjysCode,
    main
}